package com.thewell79.areadiary.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.test.UiThreadTest;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import com.android.volley.Request;
import com.dexafree.materialList.cards.BasicButtonsCard;
import com.dexafree.materialList.cards.SimpleCard;
import com.dexafree.materialList.controller.OnDismissCallback;
import com.dexafree.materialList.model.Card;
import com.dexafree.materialList.view.MaterialListView;
import com.melnykov.fab.FloatingActionButton;
import com.thewell79.areadiary.R;
import com.thewell79.areadiary.activity.EditActivity;
import com.thewell79.areadiary.request.HttpConfig;
import com.thewell79.areadiary.request.PostStringRequest;

import java.util.ArrayList;

/**
 * Created by THEWELL-LOBBY on 2015-04-27.
 */
public class MainFragment extends Fragment {

    private PostStringRequest request = new PostStringRequest(Request.Method.GET, HttpConfig.POST);

    MaterialListView listView;
    FloatingActionButton actionButton;
    SwipeRefreshLayout swipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_main, container, false);

        listView = (MaterialListView) v.findViewById(android.R.id.list);
        actionButton = (FloatingActionButton) v.findViewById(R.id.fab);
        swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeRefreshLayout);

        setHasOptionsMenu(true);

        return v;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), EditActivity.class);
            startActivity(intent);
        }
    };

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.menu_main, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ArrayList<String> dummyValues = new ArrayList<>();

        BasicButtonsCard card = new BasicButtonsCard(getActivity());


        for (int i = 0; i < 100; i++) {
            card.setTitle("제목");
            card.setDescription("Description ");
            card.setRightButtonText("확인");
            card.setLeftButtonText("취소");
            listView.add(card);
        }

        listView.setOnDismissCallback(new OnDismissCallback() {
            @Override
            public void onDismiss(final Card card, int i) {
                AlertDialog dialog = new AlertDialog.Builder(getActivity())
                        .setTitle("메시지 삭제")
                        .setMessage("삭제하시겠습니까?")
                        .setPositiveButton("확인", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                card.setDismissible(true);
                            }
                        })
                        .setNegativeButton("취소", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                card.setDismissible(false);
                            }
                        })
                        .create();

                dialog.show();
            }
        });

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);

                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    swipeRefreshLayout.setRefreshing(false);
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(android.R.color.holo_orange_light));

        actionButton.attachToRecyclerView(listView);
        actionButton.setOnClickListener(onClickListener);
    }


}
